<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateTables extends Migration {

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        if (!Schema::hasTable('users'))
        {
            Schema::create('users', function(Blueprint $table) {
                $table->engine = 'InnoDB';
                $table->increments('id');
                $table->string('first_name', 20);
                $table->string('last_name', 20);
                $table->string('email', 100)->unique();
                $table->string('password', 64);
                $table->string('remember_token', 100)->nullable();
                $table->tinyInteger('status', false);
                $table->timestamps();
            });
        }

        if (!Schema::hasTable('category'))
        {
            Schema::create('category', function(Blueprint $table) {
                $table->engine = 'InnoDB';
                $table->increments('id');
                $table->string('category_name', 100);
                $table->string('category_description');
                $table->integer('sort');
                $table->tinyInteger('status', false);
                $table->boolean('delete_flag')->default(false);
                $table->timestamps();
            });
        }

        if (!Schema::hasTable('manufacturer'))
        {
            Schema::create('manufacturer', function(Blueprint $table) {
                $table->engine = 'InnoDB';
                $table->increments('id');
                $table->string('manufacturer_name', 100);
                $table->string('manufacturer_description');
                $table->string('manufacturer_logo_main');
                $table->string('manufacturer_logo_thumbnail');
                $table->string('manufacturer_tel');
                $table->string('manufacturer_email');
                $table->integer('manufacturer_country');
                $table->integer('sort');
                $table->boolean('status')->default(true);
                $table->timestamps();
            });
        }

        if (!Schema::hasTable('product'))
        {
            Schema::create('product', function(Blueprint $table) {
                $table->engine = 'InnoDB';
                $table->increments('id');
                $table->string('product_name', 100);
                $table->string('product_model', 100)->nullable();
                $table->string('product_description', 200)->nullable();
                $table->string('product_keywords', 200)->nullable();
                $table->string('product_url_seo', 200)->nullable();
                $table->text('product_content')->nullable();
                $table->integer('product_quantity');
                $table->bigInteger('product_price');
                $table->string('product_image_main');
                $table->string('product_image_thumbnail');
                $table->integer('created_id')->unsigned();
                $table->integer('category_id')->unsigned();
                $table->integer('manufacturer_id')->unsigned();
                $table->foreign('manufacturer_id')->references('id')->on('manufacturer');
                $table->foreign('created_id')->references('id')->on('users');
                $table->foreign('category_id')->references('id')->on('category');
                $table->integer('sort');
                $table->boolean('delete_flag')->default(false);
                $table->boolean('status')->default(true);
                $table->timestamps();
            });
        }

        if (!Schema::hasTable('customer'))
        {
            Schema::create('customer', function(Blueprint $table) {
                $table->engine = 'InnoDB';
                $table->increments('id');
                $table->string('customer_name', 100);
                $table->string('customer_address');
                $table->string('customer_tel1', 18);
                $table->string('customer_tel2', 18)->nullable();
                $table->string('customer_tel3', 18)->nullable();
                $table->string('customer_email', 50)->nullable();
                $table->string('customer_image_main', 50)->nullable();
                $table->string('customer_image_thumbnail', 50)->nullable();
                $table->tinyInteger('customer_type')->default(1);
                /*
                 * 1: person
                 * 2: company
                 * */
                $table->timestamps();
            });

        }

        if (!Schema::hasTable('order'))
        {
            Schema::create('order', function(Blueprint $table) {
                $table->engine = 'InnoDB';
                $table->increments('id');
                $table->integer('customer_id')->unsigned();
                $table->integer('user_book_id')->unsigned();
                $table->integer('user_process_id')->unsigned();
                $table->tinyInteger('status');
                $table->foreign('customer_id')->references('id')->on('customer');
                $table->foreign('user_book_id')->references('id')->on('users');
                $table->foreign('user_process_id')->references('id')->on('users');
                $table->integer('sort');
                $table->timestamps();
            });
        }

        if (!Schema::hasTable('order_detail'))
        {
            Schema::create('order_detail', function(Blueprint $table) {
                $table->engine = 'InnoDB';
                $table->increments('id');
                $table->integer('product_id')->unsigned();
                $table->integer('order_id')->unsigned();
                $table->integer('order_price');
                $table->integer('order_quantity');
                $table->foreign('product_id')->references('id')->on('product');
                $table->foreign('order_id')->references('id')->on('order');
                $table->integer('sort');
                $table->timestamps();
            });
        }

        if (!Schema::hasTable('post'))
        {
            Schema::create('post', function(Blueprint $table) {
                $table->engine = 'InnoDB';
                $table->increments('id');
                $table->string('title', 255);
                $table->text('content');
                $table->string('description')->nullable();
                $table->string('keywords')->nullable();
                $table->string('image_main');
                $table->string('image_thumbnail');
                $table->integer('sort');
                $table->timestamps();
            });
        }

        if (!Schema::hasTable('comment'))
        {
            Schema::create('comment', function(Blueprint $table) {
                $table->engine = 'InnoDB';
                $table->increments('id');
                $table->string('name', 200);
                $table->string('email', 100);
                $table->text('message');
                $table->string('description')->nullable();
                $table->string('keywords')->nullable();
                $table->tinyInteger('follow', false)->default(0);
                $table->tinyInteger('sort')->default(1);
                $table->integer('post_id')->unsigned();
                $table->foreign('post_id')->references('id')->on('post');
                $table->integer('user_id')->unsigned();
                $table->foreign('user_id')->references('id')->on('users');
                $table->timestamps();
            });
        }

        if (!Schema::hasTable('company'))
        {
            Schema::create('company', function(Blueprint $table) {
                $table->engine = 'InnoDB';
                $table->increments('id');
                $table->string('company_name', 100);
                $table->string('company_description')->nullable();
                $table->string('company_logo_main')->nullable();
                $table->string('company_logo_thumbnail')->nullable();
                $table->string('company_tel')->nullable();
                $table->string('company_email')->nullable();
                $table->integer('company_country');
                $table->integer('company_address')->nullable();
                $table->tinyInteger('sort')->default(1);
                $table->timestamps();
            });
        }

        if (!Schema::hasTable('article'))
        {
            Schema::create('article', function(Blueprint $table) {
                $table->engine = 'InnoDB';
                $table->string('article_title', 200);
                $table->string('article_content');
                $table->increments('id');
                $table->tinyInteger('status', false)->default(0);
                $table->tinyInteger('sort')->default(1);
                $table->timestamps();
            });
        }

        if (!Schema::hasTable('service'))
        {
            Schema::create('service', function(Blueprint $table) {
                $table->engine = 'InnoDB';
                $table->increments('id');
                $table->string('service_name');
                $table->text('service_content');
                $table->string('image');
                $table->tinyInteger('sort')->default(1);
                $table->tinyInteger('status')->default(0);
                $table->timestamps();
            });
        }

        if (!Schema::hasTable('staff'))
        {
            Schema::create('staff', function(Blueprint $table) {
                $table->engine = 'InnoDB';
                $table->increments('id');
                $table->string('staff_name');
                $table->string('staff_image');
                $table->text('staff_content');
                $table->string('staff_email');
                $table->string('staff_phone');
                $table->tinyInteger('status', false)->default(0);
                $table->tinyInteger('sort')->default(1);
                $table->tinyInteger('position')->default(1);
                $table->timestamps();
            });
        }

        if (!Schema::hasTable('porfolio'))
        {
            Schema::create('porfolio', function(Blueprint $table) {
                $table->engine = 'InnoDB';
                $table->increments('id');
                $table->string('porfolio_title');
                $table->string('porfolio_image');
                $table->text('porfolio_content');
                $table->tinyInteger('sort')->default(1);
                $table->timestamps();
            });
        }

        if (!Schema::hasTable('project'))
        {
            Schema::create('project', function(Blueprint $table) {
                $table->engine = 'InnoDB';
                $table->increments('id');
                $table->string('project_name');
                $table->tinyInteger('status', false)->default(0);
                $table->tinyInteger('sort')->default(1);
                $table->timestamps();
            });
        }

        if (!Schema::hasTable('project_function'))
        {
            Schema::create('project_function', function(Blueprint $table) {
                $table->engine = 'InnoDB';
                $table->string('project_function_name');
                $table->float('project_price');
                $table->float('project_price_percent_off');
                $table->increments('id');
                $table->tinyInteger('status', false)->default(0);
                $table->tinyInteger('sort')->default(1);
                $table->timestamps();
            });
        }

        if (!Schema::hasTable('contact'))
        {
            Schema::create('contact', function(Blueprint $table) {
                $table->engine = 'InnoDB';
                $table->increments('id');
                $table->string('contact_name', 100)->nullable();
                $table->string('email')->nullable();
                $table->text('content')->nullable();
                $table->tinyInteger('sort')->default(1);
                $table->tinyInteger('read')->default(0);
                $table->timestamps();
            });
        }
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        if (Schema::hasTable('company'))
        {
            Schema::drop('company');
        }

        if (Schema::hasTable('order_detail'))
        {
            Schema::drop('order_detail');
        }

        if (Schema::hasTable('order'))
        {
            Schema::drop('order');
        }

        if (Schema::hasTable('product'))
        {
            Schema::drop('product');
        }

        if (Schema::hasTable('customer'))
        {
            Schema::drop('customer');
        }

        if (Schema::hasTable('category'))
        {
            Schema::drop('category');
        }

        if (Schema::hasTable('manufacturer'))
        {
            Schema::drop('manufacturer');
        }

        if (Schema::hasTable('comment'))
        {
            Schema::drop('comment');
        }

        if (Schema::hasTable('post'))
        {
            Schema::drop('post');
        }

        if (Schema::hasTable('users'))
        {
            Schema::drop('users');
        }

        if (Schema::hasTable('contact'))
        {
            Schema::drop('contact');
        }
    }
}
